package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcBerthDAO;
import com.trkj.dao.jpa.CcCasrhistoryDAO;
import com.trkj.dao.jpa.CcPatientDAO;
import com.trkj.dao.jpa.CcWardDAO;
import com.trkj.dao.jpa.TbdepartmentDAO;
import com.trkj.pojos.CcCasrhistory;
import com.trkj.pojos.CcPatient;
import com.trkj.pojos.CcWard;
import com.trkj.pojos.Tbdepartment;
import com.trkj.vo.CcCasrhistoryVo;
import com.trkj.vo.HospVo;

@Service
@Transactional
public class CcCasrhistoryService {

	@Autowired
	private CcCasrhistoryDAO cccDAO;

	@Autowired
	private TbdepartmentDAO tbdepaDAO;
	@Autowired
	private CcWardDAO ccWardDAO;
	@Autowired
	private CcPatientDAO patientDAO;

	public List<HospVo> tongji() {
		//List<Tbdepartment> list = (List<Tbdepartment>) tbdepaDAO.findAll();
		List<CcCasrhistory> ccclist=(List<CcCasrhistory>) cccDAO.findAll();
		List<HospVo> listVo = new ArrayList<>();
		for(CcCasrhistory ccc:ccclist){
			Tbdepartment td=tbdepaDAO.findOne(Long.valueOf(ccc.getInofficeid()));
			HospVo hosp = new HospVo();
			long shul = cccDAO.queryByinofficeid(td.getDepartmentNo() + "");
			hosp.setName(td.getDepartmentName());
			hosp.setValue((int) shul);
			listVo.add(hosp);
		}
		return listVo;
	}

	public Map<String, Object> tongjiE() {
		Map<String, Object> map = new HashMap<>();
		List<Tbdepartment> list = (List<Tbdepartment>) tbdepaDAO.findAll();
		int y = list.size();
		String dname[] = new String[y];
		int i = 0;
		for (Tbdepartment td : list) {
			if (i < y) {
				dname[i] = td.getDepartmentName();
				i++;
			}
		}
		// List<Tbdepartment> list=(List<Tbdepartment>) tbdepaDAO.findAll();
		List<HospVo> listVo = new ArrayList<>();
		for (Tbdepartment td : list) {
			HospVo hosp = new HospVo();
			long shul2 = cccDAO.queryByinofficeid(td.getDepartmentNo() + "");
			hosp.setName(td.getDepartmentName());
			hosp.setValue((int) shul2);
			listVo.add(hosp);
		}
		map.put("name", dname);
		map.put("value", listVo);
		return map;
	}

	public Map<String, Object> tongjiEadmissiondate() {
		Map<String, Object> map = new HashMap<>();
		
		// List<Tbdepartment> list=(List<Tbdepartment>) tbdepaDAO.findAll();
		Date date = new Date();
		String strdate = date.getYear() + 1900 + "";
		List<HospVo> listVo = new ArrayList<>();
		String dname[] = new String[date.getMonth() + 1];
		for (int i2 = 1; i2 <= date.getMonth() + 1; i2++) {
			HospVo h = new HospVo();
			long shul = 0l;
			if (i2 < 10) {
				shul = cccDAO.queryByadmissiondate(strdate + "-0" + i2);
			} else {
				shul = cccDAO.queryByadmissiondate(strdate + "-" + i2);

			}
			System.err.println(shul);
			dname[i2-1]=i2 + "月份";
			h.setName(i2 + "月份");
			h.setValue((int) shul);
			listVo.add(h);
		}
		map.put("name", dname);
		map.put("value", listVo);
		return map;
	}
	public List<HospVo> tongjiByadmissiondate() {
		Date date = new Date();
		String strdate = date.getYear() + 1900 + "";
		List<HospVo> listVo = new ArrayList<>();
		for (int i = 1; i <= date.getMonth() + 1; i++) {
			HospVo h = new HospVo();
			long shul = 0l;
			if (i < 10) {
				shul = cccDAO.queryByadmissiondate(strdate + "-0" + i);
			} else {
				shul = cccDAO.queryByadmissiondate(strdate + "-" + i);

			}
			System.err.println(shul);
			h.setName(i + "月份");
			h.setValue((int) shul);
			listVo.add(h);
		}
		return listVo;
	}

	public int delCcCasrhistory(long[] list) {
		int y = 0;
		for (int i = 0; i < list.length; i++) {
			CcCasrhistory ccc = cccDAO.findOne(list[i]);
			String strdate1 = ccc.getOutdate().getYear() + 1900 + "";
			int strdate2 = ccc.getOutdate().getMonth() + 1;
			Date date = new Date();
			String strdate3 = date.getYear() + 1900 + "";
			int strdate4 = date.getMonth() + 1;
			if (strdate1.equals(strdate3) && strdate4 - strdate2 == 3) {
				cccDAO.delete(ccc);
				y++;
			}
		}
		return y;
	}

	public Map<String, Object> queryByAll(int page, int rows) {
		Map<String, Object> map = new HashMap<>();
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcCasrhistory> list = cccDAO.queryByCcCasrhistory(pr);
		map.put("total", cccDAO.count());
		map.put("rows", ccCasrhistoryToCcCasrhistoryVo(list));
		return map;
	}

	public List<CcCasrhistoryVo> queryByCasepatientid(String pname) {
		List<CcCasrhistoryVo> listVo = new ArrayList<CcCasrhistoryVo>();
		List<CcPatient> cplist = patientDAO.findBypname(pname);
		for (CcPatient cp : cplist) {
			List<CcCasrhistory> list = cccDAO.queryBycasepatientid(cp.getPatientid() + "");
			listVo = ccCasrhistoryToCcCasrhistoryVo(list);
		}
		return listVo;
	}

	private List<CcCasrhistoryVo> ccCasrhistoryToCcCasrhistoryVo(List<CcCasrhistory> list) {
		List<CcCasrhistoryVo> listVo = new ArrayList<CcCasrhistoryVo>();
		for (CcCasrhistory ccc : list) {
			CcCasrhistoryVo cccVo = new CcCasrhistoryVo();
			cccVo.setAdmissiondate(ccc.getAdmissiondate());
			cccVo.setCasehistory(ccc.getCasehistory());
			cccVo.setCasepatientid(ccc.getCasepatientid());
			cccVo.setCberthid(ccc.getCberthid());
			cccVo.setInofficeid(ccc.getInofficeid());
			cccVo.setDiacrisis(ccc.getDiacrisis());
			cccVo.setOutdate(ccc.getOutdate());
			cccVo.setOutdiacrisis(ccc.getOutdiacrisis());
			cccVo.setOutgenreno(ccc.getOutgenreno());
			cccVo.setOutofficeid(ccc.getOutofficeid());
			if (cccVo.getOutgenreno() != null) {
				CcWard ccward = ccWardDAO.findOne(cccVo.getOutgenreno().longValue());
				cccVo.setWid(ccward.getWardid());
				cccVo.setWname(ccward.getWname());
			}
			CcPatient cp = patientDAO.findOne(Long.valueOf(cccVo.getCasepatientid()));
			if (cp != null) {
				cccVo.setPid(cp.getPatientid());
				cccVo.setPname(cp.getPname());
				cccVo.setPsex(cp.getPsex());
				Tbdepartment tb = tbdepaDAO.findOne(Long.valueOf(cccVo.getInofficeid()));
				if (tb != null) {
					cccVo.setOid(tb.getDepartmentNo());
					cccVo.setOname(tb.getDepartmentName());
				}
			}
			listVo.add(cccVo);
		}
		return listVo;
	}
}
